package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.AmortMethodReq;
import com.jrx.anytxn.param.dto.product.AmortMethodRes;
import com.jrx.anytxn.param.entity.PrAmortMethod;
import com.jrx.anytxn.param.mapper.ext.ExtPrAmortMethodMapper;
import com.jrx.anytxn.param.service.product.IAmortMethodService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 还款方式实现类
 *
 * @author : zhenghaopu
 * 2019/12/4
*/
@Service
@CacheConfig(cacheNames = "AMORT_METHOD")
public class AmortMethodServiceImpl implements IAmortMethodService {
    private static final Logger logger = LoggerFactory.getLogger(AmortMethodServiceImpl.class);

    @Resource
    private ExtPrAmortMethodMapper extPrAmortMethodMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<AmortMethodRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrAmortMethod> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrAmortMethod> prAmortMethodList = extPrAmortMethodMapper.selectByTenId(tenantId);
        if (prAmortMethodList.isEmpty()) {
            logger.error("查询还款方式列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg());
        }
        List<AmortMethodRes> res = BeanMapping.copyList(prAmortMethodList, AmortMethodRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public AmortMethodRes findByAmortIdIdAndTenId(String amortMethodId, String tenantId) throws TxnException {
        if (StringUtils.isBlank(amortMethodId) || StringUtils.isBlank(tenantId)) {
            logger.error("查询还款方式时还款方式ID或机构号为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getCode(), TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrAmortMethod prAmortMethod = extPrAmortMethodMapper.selectByAmortMethodIdAndTenId(amortMethodId, tenantId);
        if (prAmortMethod == null) {
            logger.error("还款方式数据不存在");
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.AMORTMETHOD));
        }
        return BeanMapping.copy(prAmortMethod, AmortMethodRes.class);
    }

    @Cacheable(key="'amortMethod_'+#tenantId+'_'+#organizationId+'_'+#chargeOption")
    @Override
    public PrAmortMethod findByChargerOptionAndId(String chargeOption, String organizationId, String tenantId) throws TxnBizException {
        if (StringUtils.isBlank(chargeOption) || StringUtils.isBlank(organizationId)||StringUtils.isBlank(tenantId)) {
            logger.error("还款方式ID或机构号为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getCode(), TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrAmortMethod prAmortMethod = extPrAmortMethodMapper.selectByOrgIdAndOptionAndTenId(chargeOption, organizationId, tenantId);
        if (prAmortMethod == null) {
            logger.error("还款方式数据不存在");
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.AMORTMETHOD));
        }
        return prAmortMethod;
    }

    @CacheEvict(key="'amortMethod_'+#amortMethodReq.tenantId+'_'+#amortMethodReq.organizationId" +
            "+'_'+#amortMethodReq.chargeOption")
    @Override
    public AmortMethodRes modifyAmortMethod(AmortMethodReq amortMethodReq) {
        PrAmortMethod amortMethod = BeanMapping.copy(amortMethodReq, PrAmortMethod.class);

        extPrAmortMethodMapper.updateByAmortIdAndTenId(amortMethod);
        return BeanMapping.copy(amortMethod, AmortMethodRes.class);
    }

    @Override
    public AmortMethodRes addAmortMethod(AmortMethodReq amortMethodReq) throws TxnException {
        PrAmortMethod amortMethod = BeanMapping.copy(amortMethodReq, PrAmortMethod.class);
        //通过租户ID和还款方式ID查看是否存在此信息
        PrAmortMethod method = extPrAmortMethodMapper.selectByAmortMethodIdAndTenId(amortMethodReq.getAmortMethodId(), amortMethodReq.getTenantId());
        if (method != null) {
            logger.error("该机构下还款方式ID已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS.getCode(), TxnRespCode.DATA_EXISTS.getMsg(ErrorMsgConstant.AMORT_METHOD_EXISTS));
        }
        /*通过租户ID，机构号和还款方式查询是否已存在此信息*/
        PrAmortMethod prAmortMethod = extPrAmortMethodMapper.selectByOrgIdAndOptionAndTenId(amortMethodReq.getChargeOption(), amortMethod.getOrganizationId(), amortMethodReq.getTenantId());
        if (prAmortMethod != null) {
            logger.error("还款方式种类不能重复");
            throw new TxnException(TxnRespCode.DATA_EXISTS.getCode(), TxnRespCode.DATA_EXISTS.getMsg(ErrorMsgConstant.AMORT_METHOD_CHORGE_EXISTS));
        }
        amortMethod.setRecordVersionNumber(Constant.VERSION_NUMBER);
        try {
            amortMethod.setId(segmentService.getId(Constant.PR_AMORT_METHOD));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        extPrAmortMethodMapper.insertSelective(amortMethod);
        return BeanMapping.copy(amortMethod, AmortMethodRes.class);
    }

    @CacheEvict(key="'amortMethod_'+#tenantId+'_'+#organizationId+'_'+#amortMethod")
    @Override
    public Boolean removeByTenIdAndAmortMethodId(String tenantId, String organizationId, String amortMethod) throws TxnException {
        boolean flag = true;
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(organizationId)||StringUtils.isBlank(amortMethod)) {
            logger.error("还款方式主键ID不存在");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getCode(), TxnRespCode.NOT_EMPTY.getMsg(ErrorMsgConstant.AMORT_METHOD_ID));
        }
        int a = extPrAmortMethodMapper.deleteByTenIdAndAmortId(tenantId, organizationId, amortMethod);
        if (a < 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public List<AmortMethodRes> getAmortMethodByOrgIdAndTenId(String organizationId, String tenantId) {
        List<PrAmortMethod> prAmortMethodsList = extPrAmortMethodMapper.selectAmortMethodByOrgIdAndTenId(organizationId, tenantId);
        return BeanMapping.copyList(prAmortMethodsList, AmortMethodRes.class);
    }

    @Override
    public List<PrAmortMethod>  findAmortMethodByAmortMethodIdsAndTenId(List<String> amortMethodList, String organizationId, String tenantId) {
        return extPrAmortMethodMapper.selectAmortMethodByAmortMethodIdsAndTenId(organizationId, tenantId,amortMethodList);
    }
}
